use anyhow::{Context, Result};
use mongodb::{options::ClientOptions, Client, Database};
use std::time::Duration;

pub async fn connect_to_db() -> Result<Database> {
    let mut client_options = ClientOptions::parse(
        &std::env::var("MONGODB_URL")
            .context("No db url present!")
            .unwrap(),
    )
    .await?;
    let duration = Duration::new(5, 0);
    client_options.connect_timeout = Some(duration);
    let client = Client::with_options(client_options).unwrap();
    let db = client.database("ReadLaterBot");
    Ok(db)
}
